package com.yin.myproject.io;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Eason on 2017/8/31.
 */
public class CompareForCollection {

    public static void main(String[] args) throws IOException {
        String classPath = CompareForCollection.class.getClassLoader().getResource("").getPath();
        //管理台文件转换为集合
        List<String> glt = new ArrayList<String>(180000);
        LineIterator gltIterator = FileUtils.lineIterator(new File(classPath + "glt.log"));
        while (gltIterator.hasNext()) {
            String line = gltIterator.nextLine();
//            System.out.println(line.replaceAll("\\|","").replaceAll("\\s",""));
            glt.add(line.replaceAll("\\|", "").replaceAll("\\s", ""));
        }
        //轻易贷文件转换为集合
        List<String> qyd = new ArrayList<String>(180000);
        LineIterator qydIterator = FileUtils.lineIterator(new File(classPath + "qyd.log"));
        while (qydIterator.hasNext()) {
            String line = qydIterator.nextLine();
//            System.out.println(line.replaceAll("\\|","").replaceAll("\\s",""));
            qyd.add(line.replaceAll("\\|", "").replaceAll("\\s", ""));
        }

        List<String> diffrent = getDiffrent4(glt, qyd);
        for (String str : diffrent){
            System.out.println(str);
        }
    }

    /**
     * 获取两个List的不同元素
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> getDiffrent(List<String> list1, List<String> list2) {
        long st = System.nanoTime();
        List<String> diff = new ArrayList<String>();
        for(String str:list1)
        {
            if(!list2.contains(str))
            {
                diff.add(str);
            }
        }
        System.out.println("total times "+(System.nanoTime()-st));
        return diff;
    }

    /**
     * 获取两个List的不同元素
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> getDiffrent3(List<String> list1, List<String> list2) {
        long st = System.nanoTime();
        Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
        List<String> diff = new ArrayList<String>();
        for (String string : list1) {
            map.put(string, 1);
        }
        for (String string : list2) {
            Integer cc = map.get(string);
            if(cc!=null)
            {
                map.put(string, ++cc);
                continue;
            }
            map.put(string, 1);
        }
        for(Map.Entry<String, Integer> entry:map.entrySet())
        {
            if(entry.getValue()==1)
            {
                diff.add(entry.getKey());
            }
        }
        System.out.println("getDiffrent3 total times "+(System.nanoTime()-st));
        return list1;
    }


    private static List<String> getDiffrent4(List<String> list1, List<String> list2) {
        long st = System.nanoTime();
        Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
        List<String> diff = new ArrayList<String>();
        List<String> maxList = list1;
        List<String> minList = list2;
        if(list2.size()>list1.size())
        {
            maxList = list2;
            minList = list1;
        }
        for (String string : maxList) {
            map.put(string, 1);
        }
        for (String string : minList) {
            Integer cc = map.get(string);
            if(cc!=null)
            {
                map.put(string, ++cc);
                continue;
            }
            map.put(string, 1);
        }
        for(Map.Entry<String, Integer> entry:map.entrySet())
        {
            if(entry.getValue()==1)
            {
                diff.add(entry.getKey());
            }
        }
        System.out.println("getDiffrent4 total times "+(System.nanoTime()-st));
        return diff;

    }



}
